class Path {

    /**
     * 格式化路径,去除路径中的多余路径分隔符号
     * <p>Format the path to remove redundant path separators from the path</p>
     * @param path
     */
    formatPath=(path:string):string=>{
        let protocol=this.getProtocol(path)
        let tmpPath=path
        if (protocol!=='')
        {
            tmpPath=tmpPath.substring(tmpPath.indexOf('://')+3)
        }
        let pathData=tmpPath.split("/")
        let result=''
        for(let i in pathData){
            let str=pathData[i]
            if (str!=null&&str!==""){
                result=result+"/"+str
            }
        }
        if (protocol!=='') {
            return protocol + '://' + result
        }
        return result
    }

    /**
     * 以指定分隔字符方式格式化路径字符串
     * <p>Format the path string in a way that specifies delimited characters</p>
     * @param path
     * @param splitChar
     */
    formatByChar=(path:string,splitChar?:string):string=>{
        let chr=splitChar?splitChar:"&"
        let pathData=path.split(chr)
        let result=""
        for(let i=0;i<pathData.length;i++){
            let str=pathData[i]
            if (str&&str!==""){
                result+=chr+str
            }
        }
        return result.substring(1)
    }

    /**
     * 判断URL中是否包含协议
     * <p>Check whether the URL contains a protocol</p>
     * @param url
     */
    haveProtocol=(url:string):boolean=>{
        return url.indexOf("://")>=0
    }

    /**
     * 从URL中获取协议
     * <p>Get the protocol from the URL</p>
     * @param url
     */
    getProtocol=(url:string):string=>{
        if (this.haveProtocol(url)) {
            return url.substring(0, url.indexOf("://"))
        }
        return ""
    }

    /**
     * 组合路径
     * <p>Combine paths</p>
     * @param path1
     * @param path2
     */
    comboPath=(path1:String,path2:String):string=>{
        let result=path1+"/"+path2
        return this.formatPath(result)
    }
}

const PathUtils=new Path()
export default PathUtils